package com.starry.home.ui.main

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.drake.brv.BindingAdapter
import com.drake.brv.utils.linear
import com.drake.brv.utils.setup
import com.starry.core.base.BaseActivity
import com.starry.core.constant.GlobalConfig
import com.starry.core.utils.SpUtil
import com.starry.core.utils.ZKStatusBarUtil
import com.starry.home.R
import com.starry.home.databinding.ActivityPreviewBinding
import com.starry.home.logic.model.hospital.RegisterResponseBean
import com.starry.home.viewmodel.main.PreviewViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.text.SimpleDateFormat
import java.util.Date


/**
 * Package:com.starry.dotpen_patients.ui.main
 * Author:starr
 * Time:2024/1/28 18:09
 * Description: 预约历史界面
 */
class PreviewActivity : BaseActivity<ActivityPreviewBinding, PreviewViewModel>() {

    private lateinit var adapter: BindingAdapter
    private lateinit var registerResponseBean: RegisterResponseBean

    companion object {
        fun startAction(context: Context) {
            val intent = Intent(context, PreviewActivity::class.java)
            context.startActivity(intent)
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun initData() {
        GlobalScope.launch {
            registerResponseBean =
                mViewModel.getRegisterList(SpUtil.getIntValue(GlobalConfig.userId))
            withContext(Dispatchers.Main) {
                adapter.models = registerResponseBean.ghList
                adapter.notifyDataSetChanged()
            }
        }

    }

    override fun initView() {
        ZKStatusBarUtil.setTransparentForWindow(this)
        ZKStatusBarUtil.setDarkMode(this)
        mDataBinding.titleTopBar.baseTvTitle.text = "预约记录"
        mDataBinding.titleTopBar.baseIvBack.setOnClickListener { finish() }
        mDataBinding.titleTopBar.baseIvBack.visibility = View.VISIBLE

        adapter = mDataBinding.rvPreview.linear(reverseLayout = true, stackFromEnd = true).setup {
            addType<RegisterResponseBean.Gh>(R.layout.item_preview)
            onBind {
                val model = getModel<RegisterResponseBean.Gh>()
                val tvOrderTime = findView<TextView>(R.id.tvOrderTime)
                val tvLookTime = findView<TextView>(R.id.tvLookTime)
                val tvState = findView<TextView>(R.id.tvState)
                val ivState = findView<ImageView>(R.id.ivState)
                val clBody: ConstraintLayout = findView(R.id.clBody)
                val tvDead: TextView = findView(R.id.tvDead)
                tvLookTime.text = buildString {
                    append(
                        model.emrGhTime.substring(
                            0,
                            10
                        )
                    )
                    append("\n")
                    append(model.emrGhTime.substring(11))
                }
                tvOrderTime.text = (model.emrGhOrder.substring(5))
                tvState.text = if (model.emrGhState == 0) "待就诊" else "已就诊"
                ivState.setImageResource(if (model.emrGhState == 0) R.drawable.ic_undeal else R.drawable.ic_deal)
                @SuppressLint("SimpleDateFormat") val spf = SimpleDateFormat("yyyy-MM-dd")
                val nowTime = spf.format(Date())
                if (nowTime > model.emrGhTime.substring(0, 10)) {
                    clBody.setBackgroundColor(resources.getColor(R.color.gray_light))
                    tvDead.visibility = View.VISIBLE
                }
            }
        }
    }

    override fun getLayoutId() = R.layout.activity_preview
}